import { _decorator, Component, Node, Input, Label, Animation, EventTouch, PhysicsSystem2D, Sprite, Collider2D, Contact2DType } from 'cc';
import { GameManage } from './GameManage';

const { ccclass, property } = _decorator;

@ccclass('PlayerController')
export class PlayerController extends Component {

    @property(Node)
    leftTouch: Node = null;

    @property(Node)
    rightTouch: Node = null;

    @property(Node)
    leftStone: Node = null;

    @property(Node)
    rightStone: Node = null;

    @property(Animation)
    leftAni: Animation = null;

    @property(Animation)
    rightAni: Animation = null;

    interval: any = null;

    start() {
        // 默认隐藏玩家
        this.leftStone.active = false
        this.rightStone.active = false
    }

    update(deltaTime: number) {

    }

    startGame() {

        
    }

    onTouchStart(event: EventTouch) {
        const current = event.target as Node
        if (current.name === 'Left') {
            this.leftAni.play('left')
        } else {
            this.rightAni.play('right')
        }
    }

    onTouchCancel(event: EventTouch) {
        const current = event.target as Node
        if (current.name === 'Left') {
            this.leftAni.play('right')
        } else {
            this.rightAni.play('left')
        }
    }

    init(start: boolean){
        if(start){
            try {
                this.leftStone.active = true
            } catch (error) {}
            try {
                this.rightStone.active = true
            } catch (error) {}
            this.leftStone.setPosition(46.5, -240)
            this.rightStone.setPosition(-46.5, -240)
            this.leftTouch.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
            this.leftTouch.on(Input.EventType.TOUCH_END, this.onTouchCancel, this);
            this.rightTouch.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
            this.rightTouch.on(Input.EventType.TOUCH_END, this.onTouchCancel, this);
        }else{
            this.leftTouch.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
            this.leftTouch.off(Input.EventType.TOUCH_END, this.onTouchCancel, this);
            this.rightTouch.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
            this.rightTouch.off(Input.EventType.TOUCH_END, this.onTouchCancel, this);
        }
    }

}

